package gui.linkFinder;

import java.util.ArrayList;
import java.util.EventObject;
import java.util.List;
import java.util.StringTokenizer;
import linkFinder.FindHTMLTag;
import linkFinder.js.JSFunction;

/**
 * 
 * @author grinfeld
 *
 */
public class EndFindLinkEvent extends EventObject {
	private static final long serialVersionUID = 1L;
	protected List<String> links = new ArrayList<String>();
	protected String full_url = new String();
	protected String type = new String();
	protected String source_page = new String();
	/**
	 * 
	 * @param arg0
	 */
	public EndFindLinkEvent(Object arg0, String type, String source_page) {
		super((arg0 == null ? new String() : arg0));
		if(arg0 instanceof String){
			if(!FindHTMLTag.isNullEmptyString((String)arg0)){
				this.full_url = new String(arg0.toString());
				this.appendLink(full_url);
			}else{
				this.full_url = null;
				
			}
			this.source_page = source_page;
			this.type = type;
		}
	}
	
	public String getSourcePage(){
		return this.source_page;
	}
	
	public String getType(){
		return this.type;
	}
	
	/**
	 * gets url links without query string
	 * @return
	 */
	public String getFullUrl(){
		return this.full_url;
	}
	
	/*
	 * 
	 * @param fullLink
	 */
	protected void appendLink(String fullLink){
		//to implements parsing for parameters
		if(!FindHTMLTag.isNullEmptyString(fullLink)){
			int ques_ind = fullLink.indexOf("?");
			if(ques_ind != -1){
				String params = fullLink.substring(ques_ind+1);
				StringTokenizer tokens = new StringTokenizer(params,"&");
				int i = 0;
				while(tokens.hasMoreTokens()){
					String token = tokens.nextToken();
					int eq_ind = token.indexOf(JSFunction.EQUAL);
					String param = new String();
					String value = new String();
					if(eq_ind != -1 && eq_ind+1 < token.length()){
						param = token.substring(0,eq_ind);
						value = (eq_ind+1 == token.length() ? new String() : token.substring(eq_ind+1));
						links.add(i, param);
						links.add(i+1, value);
						i = i + 2;
					}
				}
			}else{
				ques_ind = fullLink.length();
			}
		}else{
			this.full_url = null;
		}
	}
	
	/*
	 * generates string represantation of xml as following: 
	 * <links>
	 * 		<link>
	 * 			<link_url>The full URL (includes query string, if exists)</link_url>
	 * 			<source_page></source_page>
	 * 			<site_type></site_type>
	 * 			<params>
	 * 				<param_name>First Param Name</param_name>
	 * 				<param_value>First param value</param_value>
	 * 			</params>
	 * 			<params>
	 * 				<param_name></param_name>
	 * 				<param_value></param_value>
	 * 			</params>
	 * 		</link>
	 * </links> 
	 * @return
	 */
	protected String generateXML(){
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\">\n");
		sb.append("<links>\n");
		sb.append("<link>\n");
		sb.append("<link_url>" + this.getFullUrl() + "</link_url>\n");
		if(!FindHTMLTag.isNullEmptyString(this.source_page)){
			sb.append("<source_page>" + this.getSourcePage() + "</source_page>\n");
		}else{
			sb.append("<source_page />\n");
		}
		
		if(!FindHTMLTag.isNullEmptyString(this.type)){
			sb.append("<site_type>" + this.getType() + "</site_type>\n");
		}else{
			sb.append("<site_type />\n");
		}
		if(!links.isEmpty()){
			int length = links.size();
			for(int j=0; j<length; j=j+2){
				String param = links.get(j);
				String value = links.get(j+1);
				if(!FindHTMLTag.isNullEmptyString(param)){
					sb.append("<params>\n");
					sb.append("<param_name>" + param + "</param_name>\n");
					if(!FindHTMLTag.isNullEmptyString(value)){
						sb.append("<param_value>" + value + "</param_value>\n");
					}else{
						sb.append("<param_value />\n");
					}
					sb.append("</params>\n");
				}
			}
		}else{
			sb.append("<params />\n");
		}
		sb.append("</link>\n");
		sb.append("</links>\n");
		return sb.toString();
	}
	
	/*
	 * generates string represantation of xml as following: 
	 * <links>
	 * 		<link>
	 * 			<link_url></link_url>
	 * 			<source_page></source_page>
	 * 			<site_type></site_type>
	 * 			<params>
	 * 				<param_name></param_name>
	 * 				<param_value></param_value>
	 * 			</params>
	 * 			<params>
	 * 				<param_name></param_name>
	 * 				<param_value></param_value>
	 * 			</params>
	 * 		</link>
	 * </links> 
	 * @return xml representation of link
	 */
	public String getXML(){
		return (FindHTMLTag.isNullEmptyString(this.full_url) ? this.full_url : this.generateXML());
	}
}
